package com.atguigu.day18.collections02;

import org.junit.Test;

import java.util.*;

/*
Collections:操作集合的工具类
  Collection 和 Collections 有什么区别

   批量添加
   排序
   二分查找
   反转
   获取指定元素数量

 */
public class CollectionsTest {

    @Test
    public void test01() {
        //批量添加
       /* ArrayList<String>list = new ArrayList<>();
        list.add("张三");
        list.add("李四");
        list.add("王五");*/
        ArrayList<String> list = new ArrayList<>();

        Collections.addAll(list, "张三", "李四", "王五");
        System.out.println("list = " + list);
        list.add("赵六");
        System.out.println("list = " + list);

        HashSet<String> set = new HashSet<>();
        Collections.addAll(set, "张三", "李四", "王五");

        System.out.println("set = " + set);

    }

    @Test
    public void test02() {
        ArrayList<Integer> list = new ArrayList<>();
        Collections.addAll(list, 33, 99, 66, 55, 44, 22, 11);
        System.out.println("list = " + list);
        //排序
        Collections.sort(list);
        System.out.println("list = " + list);
        //二分查找
        int index = Collections.binarySearch(list, 333);
        System.out.println("index = " + index);
        //洗牌
        Collections.shuffle(list);

        System.out.println("list = " + list);

    }

    @Test
    public void test03() {
        ArrayList<String> list = new ArrayList<>();
        Collections.addAll(list, "杨幂", "赵丽颖", "杨幂", "范冰冰", "杨幂");
        //统计元素出现的次数
        int count = Collections.frequency(list, "杨幂");
        System.out.println("count = " + count);

        System.out.println("list = " + list);//[杨幂, 赵丽颖, 杨幂, 范冰冰, 杨幂]
        //元素反转
        Collections.reverse(list);
        System.out.println("list = " + list);
        //替换元素
        Collections.replaceAll(list, "杨幂", "刘婉晴");

        System.out.println("list = " + list);


    }

    @Test
    public void test05() {
        ArrayList<String> list = new ArrayList<>();
        Collections.addAll(list, "张三", "李四", "王五");

        ArrayList<String> newList = new ArrayList<>();
        Collections.addAll(newList,"A","A","A","A");
        System.out.println("newList = " + newList);
        //集合的复制 目标集合的size > 数据源集合的size
        Collections.copy(newList,list);
        System.out.println("newList = " + newList);


    }
    @Test
    public void test06(){
        ArrayList<Integer> list = new ArrayList<>();
        Collections.addAll(list,11,33,66,99);
        //将指定集合变为不可更改的集合
        List<Integer> unmodifiableList = Collections.unmodifiableList(list);
        System.out.println("unmodifiableList = " + unmodifiableList);// [11, 33, 66, 99]

        //unmodifiableList.add(666);
       // unmodifiableList.remove(1);


    }

    @Test
    public void test07(){
        ArrayList<String> list = new ArrayList<>();
        //将线程不安全集合变为线程安全集合
        List<String> synchronizedList = Collections.synchronizedList(list);


    }


}
